package com.kcyu.sfherp.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.kcyu.sfherp.config.TimestampConverter;
import com.kcyu.sfherp.dto.LocalFileDTO;
import com.kcyu.sfherp.dto.QueryCondition;
import com.kcyu.sfherp.entity.*;
import com.kcyu.sfherp.entity.utilEntity.ExcelChzd;
import com.kcyu.sfherp.entity.utilEntity.FilterDTO;
import com.kcyu.sfherp.entity.utilEntity.PageEntity;
import com.kcyu.sfherp.service.ChzdService;
import com.kcyu.sfherp.service.ExcelService;
import com.kcyu.sfherp.service.FileServer;
import com.kcyu.sfherp.service.StatisticService;
import com.kcyu.sfherp.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@Api(tags="统计相关接口")
@RequestMapping("/statistic")
@Slf4j
public class StatisticController {

    @Autowired
    private StatisticService statisticService;

    @Autowired
    private ChzdService chzdService;

    @Autowired
    private ExcelService excelService;

    @PostMapping("/pageDetail")
    public ResponseResult pageDetail(@RequestBody FilterDTO filterDTO) {
        List<Chzd> chzds = statisticService.queryByConditions(filterDTO);
        return new ResponseResult(200, "success", chzds);
    }

    @PostMapping("/pageDetail2")
    public ResponseResult<List<Chzd>> pageDetail2(@RequestBody PageEntity<Chzd> pageEntity) {
        PageEntity<Chzd> chzdPageEntity = statisticService.queryByConditions2(pageEntity);
        return new ResponseResult(200, "success", chzdPageEntity);
    }

    @PostMapping("/advanceQueryZd")
    @ApiOperation(value = "高级查询账单", notes = "高级查询账单")
    public ResponseResult<List<Chzd>> advanceQueryZd(@RequestBody List<QueryCondition> queryConditions) {
        return new ResponseResult(200, chzdService.advanceQueryZd(queryConditions));
    }

    @PostMapping("/advanceQueryMx")
    @ApiOperation(value = "高级查询明细", notes = "高级查询明细")
    public ResponseResult<List<Chmx>> advanceQueryChmx(@RequestBody List<QueryCondition> queryConditions) {
        return new ResponseResult(200, chzdService.advanceQueryChmx(queryConditions));
    }

    @PostMapping("/advanceQueryOrderZd")
    @ApiOperation(value = "高级查询订单账单", notes = "高级查询订单账单")
    public ResponseResult<List<OrderZd>> advanceQueryOrderZd(@RequestBody List<QueryCondition> queryConditions) {
        return new ResponseResult(200, chzdService.advanceQueryOrderZd(queryConditions));
    }

    @PostMapping("/advanceQueryOrderMx")
    @ApiOperation(value = "高级查询订单明细", notes = "高级查询订单明细")
    public ResponseResult<List<OrderMx>> advanceQueryOrderMx(@RequestBody List<QueryCondition> queryConditions) {
        return new ResponseResult(200, chzdService.advanceQueryOrderMx(queryConditions));
    }

    @PostMapping("/exportAdvanceExcel")
    public ResponseResult exportAdvanceExcel(@RequestBody List<QueryCondition> queryConditions, @RequestParam String type, HttpServletResponse response) throws Exception {
        WriteCellStyle headStyle = excelService.rewriteCellStyle();
        try {
            // 这里需要设置不关闭流
            switch (type) {
                case "zd":
                    List<Chzd> chzds = chzdService.advanceQueryZd(queryConditions);
                    List<ExcelChzd> chzds1 = new ExcelChzd().converts(chzds);
                    FileServer fileServer = new FileServer();
                    LocalFileDTO localFileDTO = fileServer.generateFilePath("导出账单", ".xlsx");
                    log.info("文件：{}", localFileDTO.getPath());
                    EasyExcel
                            .write(localFileDTO.getPath(), ExcelChzd.class)
                            .registerWriteHandler(new HorizontalCellStyleStrategy(headStyle, headStyle))
                            .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                            .registerConverter(new TimestampConverter())
                            .sheet("sheet1")
                            .doWrite(chzds1);
                    return new ResponseResult(200, localFileDTO.getDownloadPath());
                case "mx":
                    FileServer fileServer1 = new FileServer();
                    LocalFileDTO localFileDTO1 = fileServer1.generateFilePath("导出明细", ".xlsx");
                    EasyExcel.write(localFileDTO1.getPath(), Chmx.class)
                            .registerWriteHandler(new HorizontalCellStyleStrategy(headStyle, headStyle))
                            .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                            .registerConverter(new TimestampConverter())
                            .sheet("sheet1")
                            .doWrite(chzdService.advanceQueryChmx(queryConditions));
                    return new ResponseResult(200, localFileDTO1.getDownloadPath());
                case "order":
                    FileServer fileServer2 = new FileServer();
                    LocalFileDTO localFileDTO2 = fileServer2.generateFilePath("导出订单账单", ".xlsx");
                    EasyExcel.write(localFileDTO2.getPath(), OrderZd.class)
                            .registerWriteHandler(new HorizontalCellStyleStrategy(headStyle, headStyle))
                            .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                            .registerConverter(new TimestampConverter())
                            .sheet("sheet1")
                            .doWrite(chzdService.advanceQueryOrderZd(queryConditions));
                    return new ResponseResult(200, localFileDTO2.getDownloadPath());
                case "orderMx":
                    FileServer fileServer3 = new FileServer();
                    LocalFileDTO localFileDTO3 = fileServer3.generateFilePath("导出订单明细", ".xlsx");
                    EasyExcel.write(localFileDTO3.getPath(), OrderMx.class)
                            .registerWriteHandler(new HorizontalCellStyleStrategy(headStyle, headStyle))
                            .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                            .registerConverter(new TimestampConverter())
                            .sheet("sheet1")
                            .doWrite(chzdService.advanceQueryOrderMx(queryConditions));
                    return new ResponseResult(200, localFileDTO3.getDownloadPath());
            }
        } catch (Exception e) {
            log.error("导出Excel失败", e);
            return new ResponseResult(500, "导出Excel失败");
        }

        return null;
    }
}
